import cv2
import sys

def tohex(color):
    if len(color) == 4:
        ret = (~color[3] << 24 | color[2] << 16 | color[1] << 8 | color[0]) & 0xFFFFFFFF
    else:
        ret = (color[2] << 16 | color[1] << 8 | color[0]) & 0xFFFFFFFF
    return hex(ret)

imgfile = sys.argv[1]
txtfile = sys.argv[2]
img = cv2.imread(imgfile, cv2.IMREAD_UNCHANGED)
print("图像的形状,返回一个图像的(行数,列数,通道数):", img.shape)
print("图像的像素数目:", img.size)
print("图像的数据类型:", img.dtype)
fh = open(txtfile, 'w')
Xlen = img.shape[1]#图片列数
Ylen = img.shape[0]#图片行数
for i in range(Ylen):
    for j in range(img.shape[1]):
        fh.write(tohex(img[i][j]) + ', ')
    fh.write('\n')
fh.close()